1 /// Credit Mrs. YakaYocha
2 ///
Sourced from - https://www.youtube.com/channel/UCHp8LZ_0-iCvl-5pjHATsgw
3 ///
Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RJ8D9FRFQF9VS
4
5 using
UnityEngine.Events;
6
7 namespace
UnityEngine.UI.Extensions
8 {
9     
[RequireComponent(typeof(ScrollRect))]
10     
[AddComponentMenu("Layout/Extensions/Vertical Scroller")]
11     
public class UIVerticalScroller : MonoBehaviour
12     {
13         
[Tooltip("Scrollable area (content of desired ScrollRect)")]
14         
public RectTransform _scrollingPanel;
15         
[Tooltip("Elements to populate inside the scroller")]
16         
public GameObject[] _arrayOfElements;
17         
[Tooltip("Center display area (position of zoomed content)")]
18         
public RectTransform _center;
19         
[Tooltip("Select the item to be in center on start. (optional)")]
20         
public int StartingIndex = -1;
21         
[Tooltip("Button to go to the next page. (optional)")]
22         
public GameObject ScrollUpButton;
23         
[Tooltip("Button to go to the previous page. (optional)")]
24         
public GameObject ScrollDownButton;
25         
[Tooltip("Event fired when a specific item is clicked, exposes index number of item. (optional)")]
26         
public UnityEvent<int> ButtonClicked;
27
28
29         
private float[] distReposition;
30         
private float[] distance;
31         
//private int elementsDistance;
32         
private int minElementsNum;
33         
private int elementLength;
34         
//private int elementHalfLength;
35         
private float deltaY;
36         
private string result;
37
38         
public UIVerticalScroller() { }
39
40         
public UIVerticalScroller(RectTransform scrollingPanel, GameObject[] arrayOfElements, RectTransform center)
41         {
42             _scrollingPanel = scrollingPanel;
43             _arrayOfElements = arrayOfElements;
44             _center = center;
45         }
46
47
48         
public void Awake()
49         {
50             
var scrollRect = GetComponent<ScrollRect>();
51             
if (!_scrollingPanel)
52             {
53                 _scrollingPanel = scrollRect.content;
54             }
55             
if (!_center)
56             {
57                 Debug.LogError(
"Please define the RectTransform for the Center viewport of the scrollable area");
58             }
59             
if (_arrayOfElements == null || _arrayOfElements.Length == 0)
60             {
61                 
var childCount = scrollRect.content.childCount;
62                 
if (childCount > 0)
63                 {
64                     _arrayOfElements =
new GameObject[childCount];
65                     
for (int i = 0; i < childCount; i++)
66                     {
67                         _arrayOfElements[i] = scrollRect.content.GetChild(i).gameObject;
68                     }
69                 }
70             }
71         }
72
73         
public void Start()
74         {
75             
if (_arrayOfElements.Length < 1)
76             {
77                 Debug.Log(
"No child content found, exiting..");
78                 
return;
79             }
80
81             elementLength = _arrayOfElements.Length;
82             distance =
new float[elementLength];
83             distReposition =
new float[elementLength];
84
85             
//get distance between buttons
86             
//elementsDistance = (int)Mathf.Abs(_arrayOfElements[1].GetComponent<RectTransform>().anchoredPosition.y - _arrayOfElements[0].GetComponent<RectTransform>().anchoredPosition.y);
87             deltaY = _arrayOfElements[
0].GetComponent<RectTransform>().rect.height * elementLength / 3 * 2;
88             Vector2 startPosition =
new Vector2(_scrollingPanel.anchoredPosition.x, -deltaY);
89             _scrollingPanel.anchoredPosition = startPosition;
90
91             
for (var i = 0; i < _arrayOfElements.Length; i++)
92             {
93                 AddListener(_arrayOfElements[i], i);
94             }
95
96             
if (ScrollUpButton)
97                 ScrollUpButton.GetComponent<Button>().onClick.AddListener(() => { ScrollUp(); });
98
99             
if (ScrollDownButton)
100                 ScrollDownButton.GetComponent<Button>().onClick.AddListener(() => { ScrollDown(); });
101
102             
if (StartingIndex > -1)
103             {
104                 StartingIndex = StartingIndex > _arrayOfElements.Length ? _arrayOfElements.Length -
1 : StartingIndex;
105                 SnapToElement(StartingIndex);
106             }
107         }
108
109         
private void AddListener(GameObject button, int index)
110         {
111             button.GetComponent<Button>().onClick.AddListener(() => DoSomething(index));
112         }
113
114         
private void DoSomething(int index)
115         {
116             
if (ButtonClicked != null)
117             {
118                 ButtonClicked.Invoke(index);
119             }
120         }
121
122         
public void Update()
123         {
124             
if (_arrayOfElements.Length < 1)
125             {
126                 
return;
127             }
128
129             
for (var i = 0; i < elementLength; i++)
130             {
131                 distReposition[i] = _center.GetComponent<RectTransform>().position.y - _arrayOfElements[i].GetComponent<RectTransform>().position.y;
132                 distance[i] = Mathf.Abs(distReposition[i]);
133
134                 
//Magnifying effect
135                 
float scale = Mathf.Max(0.7f, 1 / (1 + distance[i] / 200));
136                 _arrayOfElements[i].GetComponent<RectTransform>().transform.localScale =
new Vector3(scale, scale, 1f);
137             }
138             
float minDistance = Mathf.Min(distance);
139
140             
for (var i = 0; i < elementLength; i++)
141             {
142                 _arrayOfElements[i].GetComponent<CanvasGroup>().interactable =
false;
143                 
if (minDistance == distance[i])
144                 {
145                     minElementsNum = i;
146                     _arrayOfElements[i].GetComponent<CanvasGroup>().interactable =
true;
147                     result = _arrayOfElements[i].GetComponentInChildren<Text>().text;
148                 }
149             }
150
151             ScrollingElements(-_arrayOfElements[minElementsNum].GetComponent<RectTransform>().anchoredPosition.y);
152         }
153
154         
private void ScrollingElements(float position)
155         {
156             
float newY = Mathf.Lerp(_scrollingPanel.anchoredPosition.y, position, Time.deltaTime * 1f);
157             Vector2 newPosition =
new Vector2(_scrollingPanel.anchoredPosition.x, newY);
158             _scrollingPanel.anchoredPosition = newPosition;
159         }
160
161         
public string GetResults()
162         {
163             
return result;
164         }
165
166         
public void SnapToElement(int element)
167         {
168             
float deltaElementPositionY = _arrayOfElements[0].GetComponent<RectTransform>().rect.height * element;
169             Vector2 newPosition =
new Vector2(_scrollingPanel.anchoredPosition.x, -deltaElementPositionY);
170             _scrollingPanel.anchoredPosition = newPosition;
171
172         }
173
174         
public void ScrollUp()
175         {
176             
float deltaUp = _arrayOfElements[0].GetComponent<RectTransform>().rect.height / 1.2f;
177             Vector2 newPositionUp =
new Vector2(_scrollingPanel.anchoredPosition.x, _scrollingPanel.anchoredPosition.y - deltaUp);
178             _scrollingPanel.anchoredPosition = Vector2.Lerp(_scrollingPanel.anchoredPosition, newPositionUp,
1);
179         }
180
181         
public void ScrollDown()
182         {
183             
float deltaDown = _arrayOfElements[0].GetComponent<RectTransform>().rect.height / 1.2f;
184             Vector2 newPositionDown =
new Vector2(_scrollingPanel.anchoredPosition.x, _scrollingPanel.anchoredPosition.y + deltaDown);
185             _scrollingPanel.anchoredPosition = newPositionDown;
186         }
187     }
188 }


Credit Mrs. YakaYocha

Sourced from - https:www.youtube.comchannelUCHp8LZ_0-iCvl-5pjHATsgw

Please donate: https:www.paypal.comcgi-binwebscr?cmd=_s-xclick&hosted_button_id=RJ8D9FRFQF9VS

private int elementsDistance;

private int elementHalfLength;

get distance between buttons

elementsDistance = (int)Mathf.Abs(_arrayOfElements[1].GetComponent().anchoredPosition.y - _arrayOfElements[0].GetComponent().anchoredPosition.y);

Magnifying effect




Trò chơi đua xe động vật trong UNITY Engine 114.964 lượt xem

Gõ tìm kiếm nhanh...